<template>
    <div class="app-container" v-loading.fullscreen.lock="fullLoading">
        <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" label-width="68px">
            <el-form-item label="学院" prop="deptId">
                <el-select size="mini" @change="changeSearchDept" v-model="queryParams.deptId" placeholder="学院" clearable>
                    <el-option v-for="(v, i) in dept_list" :key="i" :label="v.label" :value="v.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="专业" prop="majorId">
                <el-select @change="changeSearchMajor" v-model="queryParams.majorId" placeholder="请先选择学院再选择专业" filterable
                    clearable>
                    <el-option v-for="(v, i) in search_major_list" :key="i" :label="v.majorName" :value="v.majorId">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="班级" prop="classId">
                <el-select v-model="queryParams.classId" placeholder="请先选择专业再选择班级" filterable clearable>
                    <el-option v-for="(v, i) in search_class_list" :key="i" :label="v.className" :value="v.classId">
                    </el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="学号" prop="stuNo">
                <el-input v-model="queryParams.stuNo" placeholder="请输入学号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="学生姓名" prop="stuName">
                <el-input v-model="queryParams.stuName" placeholder="请输入学生姓名" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="奖项" prop="code">
                <el-select v-model="queryParams.code">
                    <el-option v-for="(v, i) in search_type_list" :key="i" clearable :value="v.typeCode"
                        :label="v.typeName"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="审核状态" prop="status">
                <el-select size="mini" v-model="queryParams.status" placeholder="审核状态" clearable>
                    <el-option v-for="(v, i) in applyStatus" :key="i" :label="v.label" :value="v.value" />
                </el-select>
            </el-form-item>
            <el-form-item label=" ">
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                <el-button icon="el-icon-download" type="success" size="mini" @click="exportVClick">导出审核信息</el-button>
            </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="table_data">
            <el-table-column label="奖项" prop="typeName" align="center" width="100px" />
            <el-table-column label="学生" prop="stuName" align="center" width="100px" />
            <el-table-column label="学号" prop="stuNo" align="center" width="100px" />
            <el-table-column label="学院" prop="deptName" align="center" />
            <el-table-column label="专业" prop="majorName" align="center" />
            <el-table-column label="班级" prop="className" align="center" />
            <el-table-column label="提交时间" prop="createTime" align="center" />
            <el-table-column label="申请状态" align="center">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.audius_detail_type" :value="scope.row.applyStatus" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" width="100px">
                <template slot-scope="scope">
                    <el-button type="text" size="mini" icon="el-icon-view" @click="auditVClick(scope.row)">查看详情</el-button>
                    <el-button type="text" size="mini" icon="el-icon-edit" @click="changeVClick(scope.row)">修改状态</el-button>
                    <el-button type="text" size="mini" icon="el-icon-edit" v-if=" scope.row.typeCode.search('SJDXS') != -1 && scope.row.applyStatus == 6 "
                        @click="changeTypeVClick(scope.row)">修改奖项</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <el-dialog title="申请" :visible.sync="auditV" fullscreen :close-on-click-modal="false">
            <el-form class="detail" label-width="160px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="班级">
                            <el-input readonly size="mini" v-model="auditForm.className" class="auditForm-input"></el-input>
                        </el-form-item>
                        <el-form-item label="姓名">
                            <el-input readonly size="mini" v-model="auditForm.stuName" class="auditForm-input"></el-input>
                        </el-form-item>
                        <el-form-item label="奖项">
                            <el-input readonly size="mini" v-model="auditForm.typeName" class="auditForm-input"></el-input>
                        </el-form-item>
                        <el-form-item label="政治面貌">
                            <el-input readonly size="mini" class="auditForm-input" v-model="auditForm.zzmm"></el-input>
                        </el-form-item>
                        <el-form-item label="职务">
                            <el-input readonly size="mini" class="auditForm-input" v-model="auditForm.classPost"></el-input>
                        </el-form-item>
                        <el-form-item v-if="!isEmpty(auditForm.classtwoSure)" label="第二课堂学分认定">
                            <el-input readonly size="mini" class="auditForm-input"
                                v-model="auditForm.classtwoSure"></el-input>
                        </el-form-item>
                        <!-- <el-form-item v-if="!isEmpty(auditForm.youthPercent)" label="青年大学习学习率">
                            <el-input readonly size="mini" class="auditForm-input"
                                v-model="auditForm.youthPercent"></el-input>
                        </el-form-item> -->
                        <el-form-item v-if="!isEmpty(auditForm.bankCard)" label="中国农业银行银行卡号">
                            <el-input readonly size="mini" class="auditForm-input" v-model="auditForm.bankCard"></el-input>
                        </el-form-item>
                        <el-form-item v-if="!isEmpty(auditForm.bankAddr)" label="开户行">
                            <el-input readonly size="mini" class="auditForm-input" v-model="auditForm.bankAddr"></el-input>
                        </el-form-item>
                        <el-form-item label="获奖情况">
                            <el-input readonly type="textarea" :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                                class="auditForm-input" v-model="auditForm.goodHis"></el-input>
                        </el-form-item>
                        <el-form-item label="主要事迹">
                            <el-input readonly type="textarea" :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                                class="auditForm-input" v-model="auditForm.mainHis"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="查看">
                            <el-button size="mini" @click="myScoreVClick">查看学年成绩</el-button>
                            <el-button size="mini" @click="iamVClick">查看学年思想品德</el-button>
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item v-if="!isEmpty(auditForm.fdyComment)" label="辅导员审核意见">
                            <el-input readonly type="textarea" :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                                class="auditForm-input" v-model="auditForm.fdyComment"></el-input>
                        </el-form-item>
                        <el-form-item v-if="!isEmpty(auditForm.xwComment)" label="学院审核意见">
                            <el-input readonly type="textarea" :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                                class="auditForm-input" v-model="auditForm.xwComment"></el-input>
                        </el-form-item>
                        <el-form-item v-if="!isEmpty(auditForm.jwcComment)" label="学工审核意见">
                            <el-input readonly type="textarea" :maxlength="1000" :autosize="{ minRows: 10, maxRows: 10 }"
                                class="auditForm-input" v-model="auditForm.jwcComment"></el-input>
                        </el-form-item>
                    </el-col>

                </el-row>

            </el-form>

            <el-dialog :visible.sync="iamV" title="学生思想品德" append-to-body :close-on-click-modal="false">
                <el-table :data="iam_count">
                    <el-table-column label="学年" prop="stuYearName" align="center" />
                    <el-table-column label="总记录数" prop="iamCount" align="center" />
                    <el-table-column label="加分数" prop="addCount" align="center" />
                    <el-table-column label="扣分数" prop="minusCount" align="center" />
                </el-table>
            </el-dialog>

            <el-dialog :visible.sync="myScoreV" title="学生成绩" append-to-body :close-on-click-modal="false">
                <el-table :row-style="{ height: '0' }" :cell-style="{ padding: '0' }" :data="score_list">
                    <el-table-column label="学年" prop="stuYearName" align="center" />
                    <el-table-column label="平均学分绩" prop="stuScore" align="center" />
                    <el-table-column label="综合素质总成绩" prop="cphScore" align="center" />
                    <el-table-column label="体能测试成绩" prop="sportScore" align="center" />
                    <el-table-column label="平均学分绩专业排名" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.stuMajorRank + '/' + scope.row.majorCount }}
                        </template>
                    </el-table-column>
                    <el-table-column label="综合素质总成绩班级排名" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.cphClassRank + '/' + scope.row.classCount }}
                        </template>
                    </el-table-column>
                </el-table>
                <el-table :data="count_list" :row-style="{ height: '0' }" :cell-style="{ padding: '0' }">
                    <el-table-column label="学年" prop="stuYearName" align="center" />
                    <el-table-column label="课程总门数" prop="courseCount" align="center" />
                    <el-table-column label="通过门数" prop="passCount" align="center" />
                    <el-table-column label="未通过门数" prop="unpassCount" align="center" />
                </el-table>


                <el-table :data="course_list" v-loading="course_loading" :row-style="{ height: '0' }"
                    :cell-style="{ padding: '0' }">
                    <el-table-column label="学期" prop="termName" align="center" />
                    <el-table-column label="课程" prop="courseName" align="center" />
                    <el-table-column label="成绩" prop="courseScore" align="center" />
                    <el-table-column label="是否通过" align="center">
                        <template slot-scope="scope">
                            {{ scope.row.isPass == "true" ? "通过" : "未通过" }}
                        </template>
                    </el-table-column>
                </el-table>
            </el-dialog>

        </el-dialog>

        <el-dialog title="导出奖项审核结果" :visible.sync="exportV" width="500px">

            <el-select clearable placeholder="请选择学年" v-model="exportForm.stuYearId">
                <el-option v-for="(v, i) in year_list" :key="i" :value="v.id" :label="v.stuYearName"></el-option>
            </el-select>

            <el-select clearable placeholder="请选择奖项" v-model="exportForm.code">
                <el-option v-for="(v, i) in search_type_list" :key="i" :value="v.typeCode"
                    :label="v.typeName"></el-option>
            </el-select>

            <div style="font-size: small;margin: 10px 0 10px 10px; color: red;">*学年是必选项，奖项不选就默认导出全部。</div>
            <el-button @click="doExport">导出</el-button>
        </el-dialog>

        <el-dialog title="修改状态" :visible.sync="changeV">
            <el-radio-group v-model="changeForm.applyStatus">
                <el-radio :label="10">驳回(学生可以重新提交)</el-radio>
                <el-radio :label="11">拒绝(学生不可以再提交)</el-radio>
                <el-radio :label="6">学工通过</el-radio>
                <el-radio :label="12">作废</el-radio>
            </el-radio-group>
            <el-input type="textarea" placeholder="请输入理由" v-model="changeForm.jwcComment"></el-input>
            <el-button @click="changeApply">修改</el-button>
        </el-dialog>

        <el-dialog title="修改奖项" :visible.sync="changeTypeV">
            <el-select v-model="changeTypeForm.typeCode" placeholder="请选择奖项">
                <el-option v-for="(v, i) in change_type_list" :key="i" clearable :value="v.typeCode"
                    :label="v.typeName"></el-option>
            </el-select>
            <el-input type="textarea" placeholder="请输入审核意见" v-model="changeTypeForm.jwcComment"></el-input>
            <el-button @click="changeType">修改</el-button>
        </el-dialog>

    </div>
</template>
<script>
import {
    listStuScoreClassRank, listStuYearCourseScore, countStuPass, countStuIam
    , applyStatus
} from "@/api/stuCQS/good/audit";
import { listOwnClass } from "@/api/stuCQS/info-fill/stu_eva_task";
import { listEnableYear as listYear } from "@/api/stuCQS/basedata/year";
import { listSelect } from "@/api/stuCQS/lake/type";
import { listJwcAll, jwcExport, jwcAudit , changeType } from "@/api/stuCQS/lake/apply";
import { getDeptName } from "@/api/system/dept";
import { listMajors } from "@/api/stuCQS/basedata/majors";
import { listClass } from "@/api/stuCQS/basedata/class";

import * as XLSX from 'xlsx';
import { workbook2blob, openDownloadDialog } from "@/api/helpFunc";
import lodash from "lodash";
import { isEmpty } from "@/api/helpFunc";


export default {
    name: "fdy-audit",
    dicts: ['audius_detail_type'],
    data() {
        return {
            applyStatus,

            isEmpty,

            fullLoading: false,

            loading: false,
            table_data: [],
            total: 0,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                code: null,
                status: null,
                stuNo: "",
                stuName: ""
            },

            auditV: false,
            auditForm: {},
            material_list: [],

            score_list: [],
            myScoreV: false,
            course_list: [],
            courseParams: {
                pageNum: 1,
                pageSize: 10
            },
            courseTotal: 0,
            course_loading: false,

            count_list: [],

            iamV: false,
            iam_count: [],

            exportForm: {
                code: null,
                stuYearId: null
            },
            exportV: false,

            year_list: [],
            search_type_list: [],
            search_major_list: [],
            search_class_list: [],
            dept_list: [],

            changeV: false,
            changeForm: {
                jwcComment: "",
                applyStatus: null,
                id: null
            },

            changeTypeV: false,
            changeTypeForm: {
                id: null,
                typeCode: null,
                jwcComment: "经过审核，同意该生申请。"
            },
            change_type_list: []

        }
    },
    created() {
        this.listSelect();
        this.listDept();
        this.listAllStuYear();
        this.getList();
    },
    methods: {
        async changeType(){
            let sdata = { ...this.changeTypeForm };
            if (isEmpty(sdata.typeCode)) {
                this.$message.info("请选奖项");
                return;
            }
            if (isEmpty(sdata.jwcComment)) {
                this.$message.info("请输入理由");
                return;
            }

            this.fullLoading = true;
            let res = await changeType(sdata);
            this.fullLoading = false;

            if (res.code == 200) {
                this.changeTypeV = false;
                this.$message.success(res.msg);
                this.getList();
            }
        },
        async changeTypeVClick(v) {
            this.changeTypeForm.id = v.id;
            this.changeTypeV = true;
        },
        async changeApply() {
            let sdata = { ...this.changeForm };
            if (isEmpty(sdata.applyStatus)) {
                this.$message.info("请选择状态");
                return;
            }
            if (isEmpty(sdata.jwcComment)) {
                this.$message.info("请输入理由");
                return;
            }

            this.fullLoading = true;
            let res = await jwcAudit(sdata);
            this.fullLoading = false;

            if (res.code == 200) {
                this.changeV = false;
                this.$message.success(res.msg);
                this.getList();
            }
        },
        changeVClick(row) {
            this.changeForm.id = row.id;
            this.changeV = true;
        },
        async listDept() {
            let res = await getDeptName();
            this.dept_list = [...res.data];
        },
        async changeSearchDept() {
            let sdata = {
                collegeId: this.queryParams.deptId,
                pageNum: 1,
                pageSize: 100
            }
            let res = await listMajors(sdata);
            this.search_major_list = [...res.rows];
        },
        async changeSearchMajor() {
            let sdata = {
                pageNum: 1,
                pageSize: 100,
                majorId: this.queryParams.majorId
            }
            let res = await listClass(sdata);
            if (res.code == 200) {
                this.search_class_list = [...res.rows];
            }
        },
        async listSelect() {
            let res = await listSelect();
            if (res.code == 200) {
                this.search_type_list = [...res.data];
                this.change_type_list = [...res.data];
            }
        },
        async listAllStuYear() {
            let res = await listYear();
            if (res.code == 200) {
                this.year_list = [...res.rows];
            }
        },
        async listOwnClass() {
            let res = await listOwnClass();
            if (res.code == 200) {
                this.class_list = [...res.data];
            }
        },
        async doExport() {
            let sdata = { ...this.exportForm };
            if (isEmpty(sdata.stuYearId)) {
                this.$message.info("请选择学年");
                return;
            }

            this.loading = true;
            let res = await jwcExport(sdata);
            // console.log(res);
            // return;
            this.loading = false;
            if (res.code == 200) {
                let fileName = "";
                let fileHeader = [];
                let fileHeaderZh = {};
                let fileCol = [];
                fileHeader = ['stuYearName', 'deptName', 'majorName', 'className', 'stuNo', 'stuName','zzmm','classPost','sportScore', 'stuMajorRank', 'cphClassRank', 'stuMajorPer', 'cphClassPer', 'typeName', 'applyStatusEx', 'bankCard', 'bankAddr', 'goodHis', 'mainHis', 'importTime', 'teacherName'];

                let temp = [...res.data];
                let data = [];
                temp.map(x => {
                    let needAdd = {};
                    fileHeader.map(y => {
                        switch (y) {
                            case "stuMajorRank":
                                needAdd[y] = x[y] +"/" + x["majorCount"];
                                break;
                            case "cphClassRank":
                                needAdd[y] = x[y] +"/" + x["classCount"];
                                break;
                            case "stuMajorPer":
                                needAdd[y] =  Math.round(x.stuMajorRank / x.majorCount * 10000) / 100 + "%";
                                break;
                            case "cphClassPer":
                                needAdd[y] = Math.round(x.cphClassRank / x.classCount * 10000) / 100 + "%";
                                break;
                            default:
                                needAdd[y] = x[y];
                        }
                    });
                    data.push(needAdd);
                });

                fileHeaderZh = {
                    'stuYearName': "学年",
                    "deptName": "学院",
                    "majorName": "专业",
                    "className": "班级",
                    "stuNo": "学号",
                    "stuName": "姓名",
                    'zzmm':"政治面貌",
                    'classPost':"职务",
                    'sportScore':"体测成绩",
                    "stuMajorRank": "成绩专业排名",
                    "cphClassRank": "综测班级排名",
                    "stuMajorPer": "成绩专业排名百分比",
                    "cphClassPer": "综测班级排名百分比",
                    "typeName": "奖项",
                    "applyStatusEx": "审核状态",
                    "bankCard": "银行卡号",
                    "bankAddr": "开户行",
                    "goodHis": "获奖记录",
                    "mainHis": "主要事迹",
                    "importTime": "导出时间",
                    'teacherName': "辅导员"
                };

                fileCol = [
                    { wch: 15 },
                    { wch: 40 },
                    { wch: 40 },
                    { wch: 40 },
                    { wch: 10 },
                    { wch: 10 },
                    { wch: 10 },
                    { wch: 10 },
                    { wch: 10 },
                    { wch: 15 },
                    { wch: 15 },
                    { wch: 15 },
                    { wch: 15 },
                    { wch: 15 },
                    { wch: 20 },
                    { wch: 40 },
                    { wch: 40 },
                    { wch: 40 },
                    { wch: 40 },
                    { wch: 40 },
                    { wch: 20 },
                ];
                fileName = "静湖之星名单";

                const arrayWithHeader = [fileHeaderZh, ...data];

                var sheet1 = XLSX.utils.json_to_sheet(arrayWithHeader,
                    {
                        header: fileHeader,
                        skipHeader: true
                    });
                sheet1["!cols"] = fileCol;

                // 创建一个新的空的workbook
                var wb = XLSX.utils.book_new();
                // 为每一个工作簿设置名称并添加到workbook（excel表）中
                XLSX.utils.book_append_sheet(wb, sheet1, fileName);
                const workbookBlob = workbook2blob(wb);  // 辅助函数workbook2blob
                // 下载文档并添加文件名称
                openDownloadDialog(workbookBlob, fileName + ".xlsx");  // 辅助函数openDownloadDialog

            } else {
                this.$message.info("出错啦，刷新试试");
            }

        },
        async exportVClick() {
            this.exportV = true;
        },
        async getList() {
            this.loading = true;
            let res = await listJwcAll(this.queryParams);
            this.loading = false;
            if (res.code == 200) {
                this.table_data = [...res.rows];
                this.total = res.total;
            }
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        async auditVClick(v) {
            this.auditForm = { ...v };
            this.auditV = true;
        },
        async countStuIam() {
            let stuNo = this.auditForm.stuNo;
            let res = await countStuIam(stuNo);
            if (res.code == 200) {
                this.iam_count = res.data;
            }
        },

        async countStuPass() {
            let stuNo = this.auditForm.stuNo;

            let res = await countStuPass(stuNo);
            if (res.code == 200) {
                this.count_list = [...res.data];
            }
        },
        async listStuYearCourseScore() {
            let stuNo = this.auditForm.stuNo;

            this.course_loading = true;
            let res = await listStuYearCourseScore(stuNo, this.courseParams);
            this.course_loading = false;

            if (res.code == 200) {
                this.course_list = [...res.rows];
                this.courseTotal = res.total;
            }
        },
        async listStuScoreClassRank() {
            let stuNo = this.auditForm.stuNo;
            let res = await listStuScoreClassRank(stuNo);
            if (res.code == 200) {
                this.score_list = lodash.cloneDeep(res.data);
            }
        },
        iamVClick() {
            this.countStuIam();
            this.iamV = true;
        },
        myScoreVClick() {
            this.countStuPass();
            this.listStuScoreClassRank();
            this.listStuYearCourseScore();
            this.myScoreV = true;
        }
    }
}

</script>
<style lang="scss" scoped>
.addForm-input {
    width: 400px;
}

.detail {
    .el-form-item {
        margin-bottom: 0px;
    }
}
</style>